//
//  NicknameViewController.m
//  NuwaRobot
//
//  Created by Kendama on 2017/12/1.
//  Copyright © 2017年 Kendama. All rights reserved.
//

#import "NicknameViewController.h"
#import "BorderTextFieldView.h"
#import "PublicSetUserInfoApi.h"

@interface NicknameViewController ()

@property(nonatomic,strong) BorderTextFieldView *nameTextFieldView;
@property(nonatomic,strong) UIButton *confirmButton;

@end

@implementation NicknameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

#pragma mark --- 点击事件
- (void)didClickConfirmButton
{
    NSString *nickName = _nameTextFieldView.textField.text;
    if (nickName == 0) {
        [SVProgressHUD showInfoWithStatus:@"名字不能为空"];
        return;
    }
    if (nickName.length > 10) {
        [SVProgressHUD showInfoWithStatus:@"名字长度不能超过10个字符"];
        return;
    }
    
    if ([NSString isContainsTwoEmoji:nickName] ) {
        [SVProgressHUD showInfoWithStatus:@"名字不能使用表情"];
        return;
    }
    NSString *string = [nickName stringByReplacingOccurrencesOfString:@" " withString:@""];
    
    PublicSetUserInfoApi *api = [[PublicSetUserInfoApi alloc] initWithDic:@{@"nickname":string}];
    [api dtStartYTKRequestCompletionBlockWithSuccess:^(YTKBaseRequest * _Nonnull request) {
        [SVProgressHUD showSuccessWithStatus:@"修改成功"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.7 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popViewControllerAnimated:YES];
        });
    } failure:nil];
}


#pragma mark --- 懒加载
- (UIButton *)confirmButton
{
    if(!_confirmButton)
    {
        UIView *superView = self.view;
        _confirmButton = [[UIButton alloc]init];
        [superView addSubview:_confirmButton];
        [_confirmButton setTitle:@"提交" forState:UIControlStateNormal];
        [_confirmButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_confirmButton.titleLabel setFont:fontCommonSize];
        [_confirmButton setBackgroundColor:rgba(0,159,236,1)];
        _confirmButton.layer.cornerRadius = 5;
        [_confirmButton addTarget:self action:@selector(didClickConfirmButton) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _confirmButton;
}

- (BorderTextFieldView *)nameTextFieldView
{
    if(!_nameTextFieldView)
    {
        UIView *superView = self.view;
        _nameTextFieldView = [[BorderTextFieldView alloc]init];
        [superView addSubview:_nameTextFieldView];
        _nameTextFieldView.textField.placeholder = @"请输入昵称";
        [_nameTextFieldView.textField setValue:[UIColor lightGrayColor] forKeyPath:@"_placeholderLabel.textColor"];
        
        [_nameTextFieldView.textField.rac_textSignal subscribeNext:^(NSString *x) {
            NSInteger count = 10;
            if (x.length > count)
            {
                _nameTextFieldView.textField.text = [x substringToIndex:count];
            }
        }];
        
    }
    return _nameTextFieldView;
}

- (void)masLayoutSubview
{
    __weak UIView *superView = self.view;
    
    [self.nameTextFieldView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(superView).with.offset(px1920Height(300));
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(40));
        make.right.mas_equalTo(superView.mas_right).with.offset(-px1080Width(40));
        make.height.mas_equalTo(px1920Height(150));
    }];
    
    [self.confirmButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nameTextFieldView.mas_bottom).with.offset(px1920Height(150));
        make.left.mas_equalTo(superView.mas_left).with.offset(px1080Width(40));
        make.right.mas_equalTo(superView.mas_right).with.offset(-px1080Width(40));
        make.height.mas_equalTo(px1920Height(136));
    }];
    
    
}

- (void)setNavigationStyle
{
    [self setNavigationBarTitle:@"昵称"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
